package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;

import com.itheima.mapper.CheckgroupMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.ICheckgroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CheckgroupServiceImpl extends ServiceImpl<CheckgroupMapper, CheckGroup> implements ICheckgroupService {

    @Autowired
    private CheckgroupMapper checkgroupMapper;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkgroupMapper.findPage(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkgroupMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkgroupMapper.findById(id);
    }

    @Override
    public void add(CheckGroup checkGroup, List<Integer> checkItemIds) {
        try {
            checkgroupMapper.add(checkGroup);
            Integer id = checkGroup.getId();
            if (checkItemIds!=null&&checkItemIds.size()>0){
                checkgroupMapper.addCheckGroupAndCheckItem(id,checkItemIds);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void edit(CheckGroup checkGroup, List<Integer> checkItemIds) {
        try {
            checkgroupMapper.edit(checkGroup);
            int id = checkGroup.getId();
            if (checkItemIds!=null&&checkItemIds.size()>0){
                if (checkgroupMapper.checkGroupCountId(id)>0){
                    checkgroupMapper.deleteCheckGroupAndCheckItem(id);
                }
                checkgroupMapper.addCheckGroupAndCheckItem(id,checkItemIds);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteByCheckId(Integer id) {
        //删除中间表数据
        checkgroupMapper.deleteCheckGroupAndCheckItem(id);
        //删除检查组数据
        checkgroupMapper.deleteByCheckId(id);

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkgroupMapper.findAll();
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return checkgroupMapper.findCheckGroupIdsBySetmealId(id);
    }
}
